<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 幕小铺科技（大连）有限公司 [ http://www.XXX.com ]
// +----------------------------------------------------------------------
// | 2020/03/14 jason wang
// +----------------------------------------------------------------------
namespace app\erp\logic;
use think\facade\Hook;
use think\DB;
use util\Tree;
use think\facade\Debug;
use think\facade\Cache;
/**
 * vip会员列表
 * @package app\erp\model
 */
class Viplogic{

    /**
     * 首页
     * @author shiqiang
     * @return array_out(code, message, data)
     */
    public function vip_list($user_id){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
				'user_id' 		=> $user_id,
            ];
            /********行为参数赋值（*）********/
     
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/

			$list =  model('ErpMemberVip', 'model') -> order('create_time desc') -> paginate();//获取列表
			$rel_list = [];
            foreach($list as $key => $value){
				//用户相关
				$userinfo = controller('user_center/Userlogic', 'logic\user') -> get_user_info($user_id, $user_id); //用户详情
				$rel_list[$key]['userInfo'] = $userinfo['data'];
				
				//vip相关
				$rel_list[$key]['vipId'] 				= (int)$value['id'];
				$rel_list[$key]['vipType'] 				= (int)$value['vipType'];//会员类型 1月 2季 3年
				$rel_list[$key]['dueDate'] 				= (int)$value['due_date']; //vip过期时间
				$rel_list[$key]['integral'] 			= (int)$value['integral'];
				$rel_list[$key]['cumulativeSavings'] 	= (int)$value['cumulative_savings'];
				$rel_list[$key]['createTime'] 			= (string)$value['create_time'];
			}
            $result_list = [];
            $result_list['total']  			= (int)$list -> total();//总数
            $result_list['current_page']	= (int)$list -> currentPage();//页码
            $result_list['last_page']   	= (int)$list -> lastPage();//总页数
            $result_list['per_page']   		= (int)$list -> listRows();//每页条数
            $result_list['data'] 			= $rel_list;
            /********业务逻辑********/
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
			DB::commit();
            return array_out(200, 'success', $result_list);
            /********逻辑返回********/
			
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }
}